﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Logic.ClarizenSvc;

namespace Logic.ClarizenObjects
{
    public class CustomField
    {
        public String FieldName { get; set; }
        
        public String ID { get; set; }
        public String TypeName { get; set; }

        public DurationUnit? Unit { get; set; }
        public Currency? Currency { get; set; }

        public DateTime? Date { get; set; }
        public int? Integer { get; set; }
        public double? Double { get; set; }
        public Boolean? Boolean { get; set; }
        public String String { get; set; }

        public FieldValue ToFieldValue()
        {
            FieldValue fv = new FieldValue{FieldName = this.FieldName};

            if (Date.HasValue)
                fv.Value = Date.Value;
            if (!string.IsNullOrEmpty(String))
                fv.Value = String;
            else if (Currency.HasValue && Double.HasValue)
                fv.Value = new Money { Currency = Currency.Value, Value = Double.Value };
            else if (Unit.HasValue && Double.HasValue)
                fv.Value = new Duration { Unit = Unit.Value, Value = Double.Value };
            else if (this.Boolean.HasValue)
                fv.Value = this.Boolean.Value;
            else if (Integer.HasValue)
                fv.Value = Integer.Value;
            else if (Double.HasValue)
                fv.Value = Double.Value;
            else if (!string.IsNullOrEmpty(ID))
                fv.Value = new EntityId { TypeName = this.TypeName, Value = this.ID };
            else return null;

            return fv;
        }
    }
}
